/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_banner_conf.service;

import com.yuecai.modules.yc_banner_conf.domain.YcBannerConf;
import com.yuecai.modules.yc_banner_conf.repository.YcBannerConfRepository;
import com.yuecai.modules.yc_banner_conf.service.dto.YcBannerConfDto;
import com.yuecai.modules.yc_banner_conf.service.dto.YcBannerConfQueryCriteria;
import com.yuecai.modules.yc_banner_conf.service.mapstruct.YcBannerConfMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
* @website https://el-admin.vip
* @description 服务实现
* @author dm
* @date 2020-06-24
**/
@Service
@RequiredArgsConstructor
public class YcBannerConfService {

    private final YcBannerConfRepository ycBannerConfRepository;
    private final YcBannerConfMapper ycBannerConfMapper;

    
    public Map<String,Object> queryAll(YcBannerConfQueryCriteria criteria, Pageable pageable){
        Page<YcBannerConf> page = ycBannerConfRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycBannerConfMapper::toDto));
    }

    
    public List<YcBannerConfDto> queryAll(YcBannerConfQueryCriteria criteria){
        List<YcBannerConf> list = ycBannerConfRepository.findAll((root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));

        List<YcBannerConfDto> ycBannerConfDtos = ycBannerConfMapper.toDto(list);

        return ycBannerConfDtos;
    }

    
    @Transactional
    public YcBannerConfDto findById(Long id) {
        YcBannerConf ycBannerConf = ycBannerConfRepository.findById(id).orElseGet(YcBannerConf::new);
        ValidationUtil.isNull(ycBannerConf.getId(),"YcBannerConf","id",id);
        return ycBannerConfMapper.toDto(ycBannerConf);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public YcBannerConfDto create(YcBannerConf resources) {
        return ycBannerConfMapper.toDto(ycBannerConfRepository.save(resources));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(YcBannerConf resources) {
        YcBannerConf ycBannerConf = ycBannerConfRepository.findById(resources.getId()).orElseGet(YcBannerConf::new);
        ValidationUtil.isNull( ycBannerConf.getId(),"YcBannerConf","id",resources.getId());
        ycBannerConf.copy(resources);
        ycBannerConfRepository.save(ycBannerConf);
    }

    
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            ycBannerConfRepository.deleteById(id);
        }
    }

    
    public void download(List<YcBannerConfDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcBannerConfDto ycBannerConf : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("类型 关联字典项BANNER_TYPE", ycBannerConf.getType());
            map.put("是否有效 0否 1是", ycBannerConf.getIfUse());
            map.put("请求参数 json如{\"good_id\":\"123\"}", ycBannerConf.getParam());
            map.put("位置 关联字典 POSITION_TYPE", ycBannerConf.getPositionType());
            map.put("图片 ", ycBannerConf.getImageUrl());
            map.put("创建时间", ycBannerConf.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}